#include "addfrienddialog.h"

#include <QPushButton>
#include<QScrollBar>
#include"debug.h"

AddFriendDialog::AddFriendDialog(QWidget *parent)
    :QDialog(parent)
{
    layout=new QVBoxLayout();
    layout->setContentsMargins(0,0,0,0);
    layout->setSpacing(0);

    this->setWindowTitle("添加好友");
    this->setWindowIcon(QIcon(":/Resources/windowIcon.png"));
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setFixedSize(500,600);
    this->move(QCursor::pos());
    this->setStyleSheet("QDialog{background:rgb(255,255,255);}");
    this->setLayout(layout);
    this->setStyleSheet(
        "QDialog {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:1,"
        "               stop:0 #1a1a2e, stop:0.5 #16213e, stop:1 #0d0d1a);"
        "   color: #e0e0e0;"
        "   border: 1px solid #333;"
        "   border-radius: 12px;"
        "}");



    searchline = new QLineEdit();
    searchline->setStyleSheet(
        "QLineEdit {"
        "   background: rgba(45, 45, 65, 0.8);"
        "   border: 2px solid rgba(255, 255, 255, 0.1);"
        "   border-radius: 20px;"
        "   color: #e0e0e0;"
        "   font-size: 14px;"
        "   padding: 15px 20px;"
        "   selection-background-color: rgba(255, 107, 107, 0.3);"
        "}"
        "QLineEdit:focus {"
        "   border: 2px solid rgba(255, 107, 107, 0.6);"
        "   background: rgba(55, 55, 75, 0.9);"
        "}"
        "QLineEdit::placeholder {"
        "   color: rgba(255, 255, 255, 0.5);"
        "   font-style: italic;"
        "}");
    searchline->setPlaceholderText("🔍 按手机号/用户序号查找...");
    searchline->setFixedHeight(50);
    searchline->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);


    // 搜索按钮
    searchbtn = new QPushButton();
    searchbtn->setFixedSize(50, 50);
    searchbtn->setText("搜索");
    searchbtn->setStyleSheet(
        "QPushButton {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 #ff6b6b, stop:1 #06d6a0);"
        "   border: none;"
        "   border-radius: 15px;"
        "   color: white;"
        "   font-size: 12px;"
        "   font-weight: bold;"
        "}"
        "QPushButton:hover {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 #ff8e8e, stop:1 #08e6b0);"
        "}"
        "QPushButton:pressed {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 #ff4b4b, stop:1 #04c690);"
        "}");

    searchlayout = new QHBoxLayout();
    searchlayout->setContentsMargins(10, 10, 5, 10);
    searchlayout->setSpacing(10);
    searchlayout->addWidget(searchline);
    searchlayout->addWidget(searchbtn);

    scroolArea=new AddFriendScrollArea();


    layout->addLayout(searchlayout);
    layout->addWidget(scroolArea);

#if TEST_UI
    QIcon icon(":/Resources/avator1.jpg");
    QString name("小曹要涨球");
    QString signature("这是我的个性签名");
    for(int i=0;i<2;i++)
    {
        this->scroolArea->addItem(icon,name,signature);
    }
#endif

    }



    AddFriendScrollArea::AddFriendScrollArea(QWidget *parent)
        :QScrollArea(parent)
    {
        layout=new QVBoxLayout();
        layout->setContentsMargins(0,0,0,0);
        layout->setSpacing(0);
        layout->setAlignment(Qt::AlignTop);

        contain=new QWidget();
        contain->setLayout(layout);
        contain->setStyleSheet(
            "QWidget {"
            "   background: transparent;"
            "   border: none;"
            "}");


        this->setWidget(contain);
        this->setWidgetResizable(true);
        this->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
        // 美化滚动条
        this->verticalScrollBar()->setStyleSheet(
            "QScrollBar:vertical {"
            "   width: 8px;"
            "   background: rgba(255, 255, 255, 0.05);"
            "   border-radius: 4px;"
            "}"
            "QScrollBar::handle:vertical {"
            "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
            "               stop:0 #ff6b6b, stop:1 #06d6a0);"
            "   border-radius: 4px;"
            "   min-height: 30px;"
            "}"
            "QScrollBar::handle:vertical:hover {"
            "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
            "               stop:0 #ff8e8e, stop:1 #08e6b0);"
            "}"
            "QScrollBar::handle:vertical:pressed {"
            "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
            "               stop:0 #ff4b4b, stop:1 #04c690);"
            "}"
            "QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {"
            "   border: none;"
            "   background: none;"
            "   height: 0px;"
            "}");

        this->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal { height: 0px; }");
        this->setStyleSheet(
            "QScrollArea {"
            "   border: 1px solid rgba(255, 255, 255, 0.1);"
            "   border-radius: 8px;"
            "   background: rgba(45, 45, 65, 0.3);"
            "}");
    }

    void AddFriendScrollArea::addItem(const QIcon &avator, const QString &name, const QString &signature)
    {
        AddFriendScrollItem*item=new AddFriendScrollItem(this,avator,name,signature);
        layout->addWidget(item,0,Qt::AlignTop);

    }

    AddFriendScrollItem::AddFriendScrollItem(AddFriendScrollArea*owner,const QIcon &avator, const QString &name, const QString &signature, QWidget *parent)
        :owner(owner),QWidget(parent)
    {
        layout = new QHBoxLayout();
        layout->setContentsMargins(15, 10, 15, 10);
        layout->setSpacing(15);

        this->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
        this->setLayout(layout);
        // 设置项目背景样式
        this->setStyleSheet(
            "QWidget {"
            "   background: rgba(255, 255, 255, 0.05);"
            "   border: 1px solid rgba(255, 255, 255, 0.1);"
            "   border-radius: 12px;"
            "}"
            "QWidget:hover {"
            "   background: rgba(255, 255, 255, 0.1);"
            "   border: 1px solid rgba(255, 107, 107, 0.3);"
            "}");


        // 头像按钮
        this->avator = new QPushButton();
        this->avator->setFixedSize(50, 50);
        this->avator->setIcon(avator);
        this->avator->setIconSize(QSize(45, 45));
        this->avator->setStyleSheet(
            "QPushButton {"
            "   border: none;"
            "   border-radius: 12px;"
            "   background: qlineargradient(135deg, #ff6b6b, #ffa36c);"
            "}"
            "QPushButton:hover {"
            "   background: qlineargradient(135deg, #ff8e8e, #ffb78c);"
            "   border: 2px solid rgba(255, 255, 255, 0.3);"
            "}");

        // 名字标签
        nameLabel = new QLabel();
        nameLabel->setText(name);
        nameLabel->setStyleSheet(
            "QLabel {"
            "   color: #ffd166;"
            "   font-size: 16px;"
            "   font-weight: bold;"
            "   background: transparent;"
            "   border: none;"
            "}");
        nameLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);

        // 签名标签
        this->signature = new QLabel();
        this->signature->setText(signature);
        this->signature->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
        this->signature->setStyleSheet(
            "QLabel {"
            "   color: #a0a0a0;"
            "   font-size: 12px;"
            "   background: transparent;"
            "   border: none;"
            "}");


        nameSignallayout = new QVBoxLayout();
        nameSignallayout->setSpacing(5);
        nameSignallayout->setContentsMargins(0, 0, 0, 0);
        nameSignallayout->addWidget(nameLabel);
        nameSignallayout->addWidget(this->signature);
        nameSignallayout->setAlignment(Qt::AlignLeft);

        addFriendBtn = new QPushButton();
        addFriendBtn->setText("添加");
        addFriendBtn->setFixedSize(60, 30);
        addFriendBtn->setStyleSheet(
            "QPushButton {"
            "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
            "               stop:0 #4CAF50, stop:1 #45a049);"
            "   border: none;"
            "   border-radius: 8px;"
            "   color: white;"
            "   font-size: 12px;"
            "   font-weight: bold;"
            "}"
            "QPushButton:hover {"
            "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
            "               stop:0 #66BB6A, stop:1 #4CAF50);"
            "}"
            "QPushButton:pressed {"
            "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
            "               stop:0 #388E3C, stop:1 #2E7D32);"
            "}");


        layout->addWidget(this->avator,0,Qt::AlignLeft|Qt::AlignHCenter);
        layout->addLayout(nameSignallayout,1);
        layout->addWidget(addFriendBtn,0,Qt::AlignRight);


    }
